@extends('admin.layouts.app') 

@section('content')
<!--begin right-->
<div class="container">
    

    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box ">
                    <div class="box-header">
                        <h3 class="box-title">{{$menu->name}}--权限列表</h3> &nbsp;&nbsp;&nbsp;
                            
                        
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body">
                    <div class="card-box">
                    <form  role="form" action="{{ url('zadmin/permission') }}" method="POST">
                     {{ csrf_field() }}
                     
                     <input type="hidden" name="menu_id" value="{{$menu->id}}"/>    
                        <div class="box-body">
                           
                            <!-- text input -->
                            <div class="form-group">
                                <label>权限名称</label>
                                <input type="text" class="form-control" placeholder="权限名" name="name">
                            </div>
                            <div class="form-group">
                                <label>权限路径</label>
                                <input type="text" class="form-control" placeholder="权限路径" name="permission">
                            </div>
                            <div class="form-group">
                                <label>关联权限</label>
                                <input type="text" class="form-control" placeholder="添加和更新的时候会用到" name="rel_permission">
                            </div>
                           
                            

                            
                        </div>
                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">提交</button>
                            <button type="reset" class="btn btn-default pull-right">重置</button>
                        </div>
                    </form>
                    </div>

                    <div class="card-box">
                        <table id="example2" class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>No.</th>
                                <th>权限名称</th>
                                <th>权限路径</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($permissions as $data)
                                <tr>
                                    <td><label><input type="checkbox" value="{{ $data->id }}"></label> &nbsp {{ $data->id }}</td>
                                    <td>{{ $data->name }}</td>
                                    <td>{{ $data->permission }} &nbsp;&nbsp; {{ $data->rel_permission }}</td>
                                    <td>{{ $data->created_at }}</td>
                                    <td>
                                   
                                        <a href="#" data-toggle="modal" data-target="#myModal3" data-id="{{ $data->id }}">编辑</a>
                                        |
                                        <a href="#" class="delete-alert" data-id="{{ $data->id }}"> 删除</a>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        </div>
                    </div>
                
                   

                    <!-- 模态框（编辑） -->
                    <div class="modal fade" id="myModal3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                         aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form method="post" id="update-form">
                                    {{ csrf_field() }}
                                    {{ method_field('PUT') }}
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">编辑权限</h4>
                                </div>
                                <!--内容-->
                                <div class="modal-body motaikuang-tianjia">
                                    <div class="form-group">
                                        <label>权限名称</label>
                                        <input type="text" class="form-control" name="name">
                                    </div>

                                    <div class="form-group">
                                        <label>权限路径</label>
                                        <input type="text" class="form-control" placeholder="权限路径" name="permission">
                                    </div>

                                    <div class="form-group">
                                        <label>关联权限</label>
                                        <input type="text" class="form-control" placeholder="添加和更新的时候会用到" name="rel_permission">
                                    </div>
                                    
                                    <p>
                                        <button type="button" class="btn bg-olive btn-flat margin btn-update">保存</button>
                                        <button type="button" class="btn bg-orange btn-flat margin" data-dismiss="modal">取消 </button>
                                    </p>
                                </div>
                                <input name="id" value="" type="hidden"/>
                                </form>
                            </div>
                        </div>
                    </div>
                    <!-- end 模态框（编辑） -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
    </section>
</div>
<!--end right-->
@endsection
@section('js')
    <script>
        $(function () {
            

            $("#myModal3").on("show.bs.modal", function(e) {
                var button = $(e.relatedTarget);
                var _id = button.data("id");
                $(this).find(".modal-content form").attr("action", "/system/role/" + _id);
                var modal = $(this).find(".modal-body");

                $.ajax({
                    url: "{{url('zadmin/permission')}}/"+_id+'/edit',
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        if (data.rs == "true") {
                            $("input[name=id]").val(_id);  
                            modal.find("input[name=name]").val(data.data.name);
                            modal.find("input[name=permission]").val(data.data.permission);
                            modal.find("input[name=rel_permission]").val(data.data.rel_permission);
                            modal.find("select[name=system_id]").val(data.data.system_id);


                        } else {
                            swal("查询失败", data.msg, "error")
                        }
                    },
                    error: function() {
                        swal("查询失败", "请重新尝试", "error")
                    }
                })
            });

            $(".delete-alert").on("click", function() {
                var _id = $(this).data("id");
                var token = "{{ csrf_token()  }}";
                var dom = $(this).parents("tr");
                swal({
                    title: "您确定要删除此权限吗？",
                    text: "删除的权限不可恢复",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    cancelBUttonText: "取消",
                    closeOnConfirm: false,
                    closeOnCancel: true
                }, function(isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: "{{ url('zadmin/permission') }}/"+_id,
                          
                            data: {
                               
                                _token: token,                               
                            },
                            type: "DELETE",
                            dataType: "json",
                            success: function(data) {
                                if (data.rs == "true") {
                                    dom.remove();
                                    swal("删除成功", "", "success")
                                } else {
                                    swal("删除失败", data.msg, "error")
                                }
                            },
                            error: function() {
                                swal("删除失败", "请刷新重新尝试!", "error")
                            }
                        })
                    }
                })
            });

            $('.btn-update').on('click',function(){
                var _id = $('input[name=id]').val();
                var _data = $('#update-form').serialize();
                $.ajax({
                    url: "{{url('zadmin/permission')}}/" +_id,
                    type: "PUT",
                    dataType: "json",
                    data:_data,
                    success: function(data) {
                        if (data.rs == "true") {
                            alert('操作成功')
                            setTimeout(function(){  
                                window.location.reload(true);
                            },1200);
                        } else {
                            alert('操作失败');
                        }
                        $("#myModal3").modal('hide');
                    },
                    error: function() {
                        alert("操作失败")
                    }
                })       
            } );

           
        });
       
    </script>
@endsection